﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Sisocana.Core;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Dal.EntityModels;
using Sisocana.Dal;
using Sisocana.Dal.Interfaces;
using System.Data;


namespace Sisocana.Core
{
    public class ONGService : ServiceBase<IONGModel>, IONGService
    {
        public ONGService()
            : this(new ONGRepository())
        {
        }
        private IONGRepository _ONGRepository;

        public ONGService(IONGRepository ONGRepository)
        {
            _ONGRepository = ONGRepository ?? new ONGRepository();
        }

        public void createONGMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<ONG, ONGModel>()
                .ForMember(s => s.descripcion, opt => opt.MapFrom(c => c.descripcion.Trim()))
                .ForMember(s => s.estado, opt => opt.MapFrom(c => c.estado.Trim()))
                .ForMember(s => s.nombre, opt => opt.MapFrom(c => c.nombre.Trim()))
                .ForMember(s => s.urlGeneral, opt => opt.MapFrom(c => c.urlGeneral.Trim()));
        }

        public void createONGMapperModel2EF()
        {
            AutoMapper.Mapper.CreateMap<ONGModel, ONG>();
        }

        public int createONG(ONGModel ong)
        {
            createONGMapperModel2EF();
            ONG nueva = new ONG();
            ong.estado = "A";
            nueva = AutoMapper.Mapper.Map(ong, nueva);

            _ONGRepository.Add(nueva);

            try
            {
                _ONGRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }
        }

        public int updateONG(ONGModel ong)
        {
            ONG edit = _ONGRepository.GetSingle(o => o.id == ong.id);
            if (edit == null)
                return -1;
            edit.nombre = ong.nombre;
            edit.urlGeneral = ong.urlGeneral;
            edit.descripcion = ong.descripcion;

            try
            {
                _ONGRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }
        }

        public int disableONG(int ongId)
        {
            ONG disable = _ONGRepository.GetSingle(s => s.id == ongId);
            if (disable == null)
                return -1;
            if (disable.estado == "I")
                disable.estado = "A";
            else
                disable.estado = "I";

            try
            {
                _ONGRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }
        }

        public ONGModel GetSingle(Expression<Func<ONGModel, bool>> whereCondition)
        {
            createONGMapperEF2Model();

            IList<ONG> ONGsEF = _ONGRepository.GetAll();
            List<ONGModel> ONGS = new List<ONGModel>();

            ONGS = AutoMapper.Mapper.Map(ONGsEF, ONGS);

            IQueryable<ONGModel> ONGRet = ONGS.AsQueryable<ONGModel>().Where(whereCondition);

            List<ONGModel> ret = ONGRet.ToList();

            if (ret.Count == 0)
                return null;

            return ret.First();
        }

        public void Add(ONGModel entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(ONGModel entity)
        {
            throw new NotImplementedException();
        }

        public void Update(ONGModel entity)
        {
            throw new NotImplementedException();
        }

        public IList<ONGModel> GetAll(Expression<Func<ONGModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<ONGModel> GetAll()
        {
            createONGMapperEF2Model();

            IList<ONG> ONGsEF = _ONGRepository.GetAll();
            List<ONGModel> ONGs = new List<ONGModel>();
            foreach (ONG ONGEF in ONGsEF)
            {
                ONGModel ong = new ONGModel();
                ong = AutoMapper.Mapper.Map(ONGEF, ong);
                ONGs.Add(ong);
            }
            return ONGs;
        }

        public IQueryable<ONGModel> Query(Expression<Func<ONGModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count(Expression<Func<ONGModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
